﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Ricciolo.StylesExplorer.Forms;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;

namespace Ricciolo.StylesExplorer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        private static void HandleException(Exception e)
        {
            using (HandleErrorDialog he = new HandleErrorDialog(e))
            {
                if (Application.OpenForms.Count > 0)
                    he.ShowDialog(Application.OpenForms[0]);
                else
                    he.ShowDialog();
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception && !(e.ExceptionObject is COMException))
            {
                HandleException((Exception)e.ExceptionObject);
            }
        }
    }
}
